package com.clgk.wechatmall.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clgk.wechatmall.common.Response;
import com.clgk.wechatmall.common.sql.Page;
import com.clgk.wechatmall.manager.dao.ManagerDao;
import com.clgk.wechatmall.manager.entity.ManagerVo;
import com.clgk.wechatmall.manager.service.ManagerService;

@Service
@Transactional
public class ManagerServiceImpl implements ManagerService {

	@Resource ManagerDao managerDao;
	
	@Override
	public List<ManagerVo> getUserList(ManagerVo vo, Page page) {
		return managerDao.getUserList(vo, page);
	}

	@Override
	public Long saveUser(ManagerVo vo,Response<Long> response) {
		ManagerVo param = new ManagerVo();
		param.setAccount(vo.getAccount());
		List<ManagerVo> list = managerDao.getUserList(param, null);
		if(CollectionUtils.isNotEmpty(list)){
			response.setRetCode(-1);
			response.setMessage("账号已存在");
			return 0l;
		}
		response.setData(managerDao.saveUser(vo));
		return response.getData();
	}

	@Override
	public void updateUser(ManagerVo vo,Response<Long> response) {
		if(StringUtils.isNotBlank(vo.getAccount())){
			ManagerVo param = new ManagerVo();
			param.setAccount(vo.getAccount());
			List<ManagerVo> list = managerDao.getUserList(param, null);
			if(CollectionUtils.isNotEmpty(list) && list.get(0).getId().longValue() != vo.getId().longValue()){
				response.setRetCode(-1);
				response.setMessage("账号已存在");
				return;
			}
		}
		response.setData(managerDao.updateUser(vo));
	}

	@Override
	public Integer delUser(String ids) {
		return managerDao.delUser(ids);
	}


	@Override
	public ManagerVo login(ManagerVo u) {
		return managerDao.getUser(u.getAccount(), u.getPassword());
	}

}
